package cn.openea.perpartner.meily.web.service.importandexport;

import cn.openea.perpartner.common.core.exception.ServiceException;
import cn.openea.perpartner.common.core.utils.poi.ExcelUtil;
import cn.openea.perpartner.meily.constant.CommonConstants;
import cn.openea.perpartner.meily.constant.enums.ExpressCompany;
import cn.openea.perpartner.meily.constant.enums.LogisticsModule;
import cn.openea.perpartner.meily.web.entity.ProvideSamplesSub;
import cn.openea.perpartner.meily.web.entity.dto.ImportErrMsgDTO;
import cn.openea.perpartner.meily.web.entity.dto.ProvideSamplesSubDTOFactory;
import cn.openea.perpartner.meily.web.entity.dto.ProvideSamplesSubDTOFactory.SamplesShipmentTemplateDTO;
import cn.openea.perpartner.meily.web.entity.vo.ExportDataVo;
import cn.openea.perpartner.meily.web.entity.vo.ProvideSamplesSubVo;
import cn.openea.perpartner.meily.web.service.IProvideSamplesSubService;
import cn.openea.perpartner.meily.web.service.logistics.businessModule.LogisticsModlueHandler;
import cn.openea.perpartner.meily.web.service.logistics.businessModule.LogisticsModuleService;
import com.alibaba.fastjson.JSONObject;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ysk
 * @version 1.0.1
 * @Description 样品批量发货
 * @createTime 2023年05月17日 00:31:00
 */
@Slf4j
@Service(value = CommonConstants.IMPORTANDEXPORT.SAMPLE_BATCH_DELIVERY)
public class SampleBatchDeliveryService extends ImportAndExportAbstractService {

    @Resource
    private IProvideSamplesSubService provideSamplesSubService;
    @Resource
    private ProvideSamplesSubDTOFactory provideSamplesSubDTOFactory;
    @Resource
    private LogisticsModlueHandler logisticsModlueHandler;

    @Override
    public void exportExcel(ExportDataVo exportDataVo, HttpServletResponse response) {
        ProvideSamplesSubVo samplesSubVo = JSONObject.parseObject(JSONObject.toJSONString(exportDataVo.getQueryVO()),
                ProvideSamplesSubVo.class);
        if (samplesSubVo == null) {
            samplesSubVo = new ProvideSamplesSubVo();
        }
        // 发货模板默认查询的是待发货的数据
        samplesSubVo.setOrderStatus("待发货");
        List<ProvideSamplesSub> provideSamplesSubs = provideSamplesSubService.findListByVo(samplesSubVo);
        List<SamplesShipmentTemplateDTO> shipmentTemplateDTOS = provideSamplesSubs
                .stream()
                .map(item -> provideSamplesSubDTOFactory.toSamplesShipmentTemplateDTO.apply(item))
                .collect(Collectors.toList());
        ExcelUtil<SamplesShipmentTemplateDTO> util = new ExcelUtil<>(SamplesShipmentTemplateDTO.class);
        util.exportExcel(response, shipmentTemplateDTOS, "样品发货模版.xlsx");
    }

    @Override
    protected ImportErrMsgDTO handleSingleExcel(@NonNull Object obj) {
        SamplesShipmentTemplateDTO shipmentTemplateDTO = (SamplesShipmentTemplateDTO) obj;
        try {
            if (StringUtils.isBlank(shipmentTemplateDTO.getConsigneeName()) || StringUtils.isBlank(shipmentTemplateDTO.getCourierNumber())) {
                throw new ServiceException("快递公司或快递单号为空");
            }
            ExpressCompany expressCompany = ExpressCompany.findMostSimilarElement(shipmentTemplateDTO.getCourierName());
            LogisticsModuleService service = logisticsModlueHandler.getService(LogisticsModule.SAMPLES);
            service.logisticsChange(expressCompany, shipmentTemplateDTO.getId(), shipmentTemplateDTO.getCourierNumber());
        } catch (Exception e) {
            return ImportErrMsgDTO.builder()
                    .id(shipmentTemplateDTO.getId())
                    .errMsg(e.getMessage())
                    .build();
        }
        return null;
    }
}
